#include <QObject>
#include <QByteArray>
#include <QString>
#include <QDate>
#include <QTextCodec>
#include <QDebug>
#include <QFile>

class LogReadWorker : public QObject {
    Q_OBJECT
public:
    LogReadWorker() {

    }
    QString getCorrectUnicode(const QByteArray &ba) {
        QTextCodec::ConverterState state;
        QTextCodec *codec = QTextCodec::codecForName("UTF-8");
        QString text = codec->toUnicode(ba.constData(), ba.size(), &state);
        if (state.invalidChars > 0) {
            text = QTextCodec::codecForName("GBK")->toUnicode(ba);
        } else {
            text = ba;
        }
        return text;
    }
signals:
    void finish(QString str);
private slots:
    void onTimeout() {
        QString str;
        QDate date = QDate::currentDate();
        QString dateStr = date.toString("yyyy-MM-dd");
        QFile file("./tomcat/logs/catalina."+dateStr+".log");
        if (file.open(QIODevice::ReadOnly)) {
            while(!file.atEnd()) {
                str += getCorrectUnicode(file.readLine());
            }
            emit finish(str);
        } else {
          qDebug() << "open log failed";
        }
    }
};
